package server;

import server.container.Context;
import server.container.Engine;
import server.container.Host;
import server.container.Wrapper;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

public class RequestProcessor extends Thread {

    private Socket socket;
    private Engine engine;

    public RequestProcessor(Socket socket, Engine engine) {
        this.socket = socket;
        this.engine = engine;
    }

    @Override
    public void run() {
        try{
            InputStream inputStream = socket.getInputStream();
            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());
            String removeAddr = request.getRemoveAddr();
            Host host = engine.getHostMap().get(removeAddr);
            if(null == host){
                if("localhost".equals(removeAddr.trim())){
                    host = engine.getHostMap().get("127.0.0.1");
                }else if("127.0.0.1".equals(removeAddr.trim())){
                    host = engine.getHostMap().get("localhost");
                }
            }

            if(null == host){
                response.output(HttpProtocolUtil.getHttpHeader404());
            }else{
                Wrapper wrapper = null;
                Context context= getContext(request,host.getContextMap());
                if(null == context){
                    response.output(HttpProtocolUtil.getHttpHeader404());
                }else{
                    wrapper = context.getWrapperMap().get(request.getUrl());
                    if(null == wrapper){
                        response.outputHtml(host.getAppBase(),request.getContent(),request.getUrl());
                    }else{
                        Servlet httpServlet = wrapper.getServlet();
                        httpServlet.service(request,response);
                    }
                }

            }

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private Context getContext(Request request, Map<String,Context> contextMap){
        // /demo/lagou
        String url  = request.getUrl();
        Context  context = null;
        String content = "";

        for(Map.Entry<String,Context> entry:contextMap.entrySet()){
            // demo   /demo/lagou
            if(url.indexOf("/"+entry.getKey()+"/") !=-1){
                content = entry.getKey();
                context = entry.getValue();
                break;
            }
        }
        if(null == context){
            content = "/";
            context = contextMap.get(context);
            request.setContent("");
        }else{
            request.setContent("/"+content);
            request.setUrl(url.replace("/"+content,""));
        }

        return context;
    }
}
